package com.pb.wkflow.core.listener;

import com.pb.wkflow.core.config.DeleteReasonConstants;
import com.pb.wkflow.core.utils.WorkflowThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.camunda.bpm.engine.impl.persistence.entity.TaskEntity;

import java.util.List;

/**
 * @author 苗益辉
 * @date 2022/9/28 16:46
 */
@Slf4j
public class TaskDeleteListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        List<String> reBackIds = (List) WorkflowThreadLocalUtil.get("reBack");
        if (CollectionUtils.isNotEmpty(reBackIds)) {
            for (String rebackId : reBackIds) {
                if (delegateTask.getId().equals(rebackId)) {
                    delegateTask.setAssignee(null);
                    ((TaskEntity) delegateTask).delete(DeleteReasonConstants.REBACK, false);
                    break;
                }
            }
        }
    }
}
